/**
 * 
 */
package com.bloomingstars.sqm.util;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import com.bloomingstars.sqm.SqmReadingRecord;

/**
 * @author Emmanuele Sordini
 */
public class SqmReadingsGenerator {

    private Calendar start;
    private double frequency; // s
    private double[] holePositions; // % position
    private double totalDuration; // s
    private double averageReading;
    private boolean noise;
    private double rampingDuration;
    private List<SqmReadingRecord> readings = new ArrayList<SqmReadingRecord>();
    
    public void calculateReadings() {
        double time = start.getTimeInMillis();
        double readingCount = totalDuration/frequency; 
        List<Integer>holeIntPos = new ArrayList<Integer>();
        int rampingDurationInt = (int)(rampingDuration/frequency);
        
        for (int i=0;i<readingCount;i++) {
            Calendar cal = Calendar.getInstance(Locale.US);
            cal.setTimeInMillis((long)time);
            SqmReadingRecord record = new SqmReadingRecord(cal, averageReading, 20.0);
            readings.add(record);
            time += (frequency*1000);
        }
        
        for (int k=0; k<holePositions.length; k++) {
            double hole = holePositions[k];
            holeIntPos.add((int)(hole*readingCount));
        }
        
        for (Integer holePos: holeIntPos) {
            int startIndex = holePos - rampingDurationInt;
            int endIndex = holePos + rampingDurationInt;
            
            double delta = (averageReading - 6.0) / rampingDurationInt;
            double reading = 6.0;
            
            for (int j=startIndex; j<endIndex; j++) {
                if (j>0 && j<readings.size()) {
                    if (j < holePos) {
                        reading = readings.get(j).getSqmReading() - (j-startIndex)*delta;
                    } else {
                        reading = readings.get(j).getSqmReading() - (endIndex - j)*delta;
                    }

                    readings.get(j).setSqmReading(reading);
                }
            }                       
        }
    }
    
    public double getRampingDuration() {
        return rampingDuration;
    }

    public void setRampingDuration(double rampingDuration) {
        this.rampingDuration = rampingDuration;
    }

    public Calendar getStart() {
        return start;
    }
    public void setStart(Calendar start) {
        this.start = start;
    }
    public double getFrequency() {
        return frequency;
    }
    public void setFrequency(double frequency) {
        this.frequency = frequency;
    }
    public double[] getHolePositions() {
        return holePositions;
    }
    public void setHolePositions(double[] holePositions) {
        this.holePositions = holePositions;
    }
    public double getTotalDuration() {
        return totalDuration;
    }
    public void setTotalDuration(double totalDuration) {
        this.totalDuration = totalDuration;
    }
    public double getAverageReading() {
        return averageReading;
    }
    public void setAverageReading(double averageReading) {
        this.averageReading = averageReading;
    }
    public boolean isNoise() {
        return noise;
    }
    public void setNoise(boolean noise) {
        this.noise = noise;
    }
    public List<SqmReadingRecord> getReadings() {
        return readings;
    }
    
}
